<?php
class Lang{
	private static $path = 'app/lang';
	private static $languages = array();
	private static $current = null;
	
	public static function languageExists($lang){
		return file_exists(self::$path.'/lang.'.$lang.'.php');
	}
	
	public static function load($lang){
		if(self::languageExists($lang)){
			if(!isset(self::$languages[$lang])){
				self::$languages[$lang] = include(self::$path.'/lang.'.$lang.'.php');
			}
			self::setLocale($lang);
		}
	}
	
	public static function setLocale($locale){
		if(!isset(self::$languages[$locale])){
			self::load($locale);
		}else{
			self::$current = $locale;
		}
	}
	
	public static function get($key, $vars = array()){
		$str = (isset(self::$languages[self::$current][$key]) ? self::$languages[self::$current][$key] : $key);
		if(count($vars) == 0) return $str;		
		foreach($vars as $key => $val){
			$str = str_replace('{'.$key.'}', $val, $str);
		}
		return $str;
	}
	
	public static function has($key){
		return isset(self::$languages[self::$current][$key]);
	}
}

?>